package global
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.utils.Dictionary;
	
	import view.components.TempBitmap;
	
	

	public class Resource
	{
		public static var roomBg:DisplayObject;
		private static var resource:*;
		public static var uicomponent:*;
		public static var effect:*;
		public static var logo:DisplayObject;
		
		private static var exsitTAnimation:Dictionary = new Dictionary();
		
		public static var exsitTempBitmaps:Array = [];
		
		

		
		public static function setResource(resource:*):void
		{
			Resource.resource = resource;
			
			for each (var tempBitmap:TempBitmap in exsitTempBitmaps)
			{
				tempBitmap.startLoad();
			}
		}
		
		public static function addTempBitmap(tb:DisplayObject):void
		{
			if (tb is TempBitmap)
			{
				exsitTempBitmaps.push(tb);
			}
		}
		
		public static function removeTempBitmap(tb:DisplayObject):void
		{
			var index:int = exsitTempBitmaps.indexOf(tb);
			
			if ( index >= 0)
			{
				exsitTempBitmaps.splice(index,1);
			}
		}
		
		public static function getTableAnimation(type:String):MovieClip
		{
			if (exsitTAnimation[type] == null)
			{
				exsitTAnimation[type] = getEffect(type);
			}
			return exsitTAnimation[type];
		}
		
		public static function getSound(soundType:String):Sound
		{
			soundType = soundType.toUpperCase();
			return effect.sound[soundType];
		}
		
		public static function getShape(shapeKey:String):Sprite
		{
		
			if (effect.shape[shapeKey] == null)
			{
				return null;
			}
			
			return new effect.shape[shapeKey]();
		}
		
		public static function getEmote(emoteKey:String):MovieClip
		{
			return new effect.emote[emoteKey]();
		}
		
		public static function getEffect(effectKey:String):MovieClip
		{
			
			return new effect.dic[effectKey]();
		}
		
		public static function getImg(imgKey:String,smooth:Boolean = false):Bitmap
		{
			
			if (resource != null)
			{
				return new Bitmap(getBitmapData(imgKey),"auto",smooth);
			}
			
			var tb:TempBitmap = new TempBitmap(imgKey,smooth);
			addTempBitmap(tb);
			return tb;
		}
		
		public static function getImgXY(x:int,y:int,imgKey:String,smooth:Boolean = false):Bitmap
		{
			var bitmap:Bitmap = getImg(imgKey,smooth);
			bitmap.x = x;
			bitmap.y = y;
			return bitmap;
		}
		
		public static function getCopyImg(imgKey:String):Bitmap
		{
			return new Bitmap(getBitmapData(imgKey).clone());
		}
		
		public static function getBitmapData(imgKey:String):BitmapData
		{
			var bitmapData:BitmapData = resource.getResource(imgKey);
			if (bitmapData == null)
			{
				throw Error("缺少图片 "+ imgKey);
			}
			return bitmapData;
		}
		
		public static function getItemAnimate(animateId:String):DisplayObject
		{
			if (effect.item[animateId])
			{
				return new effect.item[animateId]();
			}
			return null;
		}
		
		
	}
}